package com.lagou.myblog.util;

import org.joda.time.DateTime;
import org.joda.time.Days;
import org.joda.time.Minutes;
import org.joda.time.format.DateTimeFormat;
import org.springframework.util.StringUtils;

import java.util.Calendar;
import java.util.Date;

/**
 * 时间工具
 */
public class DateHelper {
    public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    /**
     * 判断两个日期是否在同一天
     */
    public static boolean isSameDay(String date, String anotherDate) {
        boolean res = false;
        // 初始化时间
        DateTime dateTime = new DateTime();

        DateTime dt1 = new DateTime(date);
        DateTime dt2 = new DateTime(anotherDate);

        int intervalDays = Days.daysBetween(dt1, dt2).getDays();

        if (intervalDays == 0) {
            res = true;
        }
        return res;
    }

    /**
     * 获取当前系统日期
     */
    public static String getCurSysDate() {
        // 初始化时间
        DateTime dateTime = new DateTime();
        return dateTime.toString("yyyy-MM-dd");
    }

    /**
     * 获取当前系统日期时间
     */
    public static String getCurSysDatetime() {
        // 初始化时间
        DateTime dateTime = new DateTime();
        return dateTime.toString(DateHelper.YYYY_MM_DD_HH_MM_SS);
    }

    /**
     * 获取当前系统时间的年月
     */
    public static String getCurSysYearMonth() {
        // 初始化时间
        DateTime dateTime = new DateTime();
        return dateTime.toString("yyyy年MM月");
    }

    /**
     * 获取当前系统时间的年
     */
    public static String getCurSysYear() {
        // 初始化时间
        DateTime dateTime = new DateTime();
        return dateTime.toString("yyyy");
    }

    /**
     * 根据日期获得星期
     */
    public static String getWeekOfDate(Date date) {
        String[] weekDaysName = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
//        String[] weekDaysCode = {"0", "1", "2", "3", "4", "5", "6"};
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int intWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        return weekDaysName[intWeek];
    }

    /**
     * 根据日期<String>获得【星期】
     */
    public static String getWeekOfDateStr(String date) {
        // 返回值
        String res = "";
        DateTime dateTime = null;
        if (!StringUtils.isEmpty(date)) {
            dateTime = DateTimeFormat.forPattern(DateHelper.YYYY_MM_DD_HH_MM_SS).parseDateTime(date);
        }
        int dayOfWeek = dateTime.getDayOfWeek();

        switch (dayOfWeek) {
            case 1:
                res = "周一";
                break;
            case 2:
                res = "周二";
                break;
            case 3:
                res = "周三";
                break;
            case 4:
                res = "周四";
                break;
            case 5:
                res = "周五";
                break;
            case 6:
                res = "周六";
                break;
            case 7:
                res = "周日";
                break;

            default:
                break;
        }
        return res;
    }

    /**
     * 根据日期<String>获得【月份】
     */
    public static String getMonthOfDateStr(String date) {
        // 返回值
        String res = "";
        DateTime dateTime = null;
        if (!StringUtils.isEmpty(date)) {
            dateTime = DateTimeFormat.forPattern(DateHelper.YYYY_MM_DD_HH_MM_SS).parseDateTime(date);
        }
        int monthOfYear = dateTime.getMonthOfYear();
        res = String.valueOf(monthOfYear);
        return res;
    }

    /**
     * 根据日期<String>获得【年份】
     */
    public static String getYearOfDateStr(String date) {
        // 返回值
        String res = "";
        DateTime dateTime = null;
        if (!StringUtils.isEmpty(date)) {
            dateTime = DateTimeFormat.forPattern(DateHelper.YYYY_MM_DD_HH_MM_SS).parseDateTime(date);
        }
        int year = dateTime.getYear();
        res = String.valueOf(year);
        return res;
    }

    /**
     * 根据日期<年份月份>获得【年份】
     */
    public static String getYearOfYearMonth(String date) {
        // 返回值
        String res = "";
        DateTime dateTime = null;
        if (!StringUtils.isEmpty(date)) {
            dateTime = DateTimeFormat.forPattern("yyyy年MM月").parseDateTime(date);
        }
        int year = dateTime.getYear();
        res = String.valueOf(year);
        return res;
    }

    /**
     * 根据日期<年份月份>获得【月份】
     */
    public static String getMonthOfYearMonth(String date) {
        // 返回值
        String res = "";
        DateTime dateTime = null;
        if (!StringUtils.isEmpty(date)) {
            dateTime = DateTimeFormat.forPattern("yyyy年MM月").parseDateTime(date);
        }
        int monthOfYear = dateTime.getMonthOfYear();
        res = String.valueOf(monthOfYear) + "月";
        return res;
    }

    /**
     * 根据日期<String>获得【月-日】
     */
    public static String getMonthAndDayOfDateStr(String date) {
        // 返回值
        String res = "";
        DateTime dateTime = null;
        if (!StringUtils.isEmpty(date)) {
            dateTime = DateTimeFormat.forPattern(DateHelper.YYYY_MM_DD_HH_MM_SS).parseDateTime(date);
        }
        int monthOfYear = dateTime.getMonthOfYear();
        int dayOfMonth = dateTime.getDayOfMonth();
        res = monthOfYear + "-" + dayOfMonth;
        return res;
    }

    /**
     * 根据日期<String>获得【年-月】
     */
    public static String getYearAndMonthOfDateStr(String date) {
        // 返回值
        String res = "";
        String month = "";
        DateTime dateTime = null;
        if (!StringUtils.isEmpty(date)) {
            dateTime = DateTimeFormat.forPattern(DateHelper.YYYY_MM_DD_HH_MM_SS).parseDateTime(date);
        }
        int year = dateTime.getYear();
        int monthOfYear = dateTime.getMonthOfYear();
        if (monthOfYear < 10) {
            month = "0" + monthOfYear;
        } else {
            month = String.valueOf(monthOfYear);
        }
        res = year + "年" + month + "月";
        return res;
    }

    /**
     * 将时间字符串转换成DateTime对象<yyyy-MM-dd HH:mm:ss>
     */
    public static DateTime str2DateTime(String date) {
        DateTime dateTime = null;
        if (!StringUtils.isEmpty(date)) {
            dateTime = DateTimeFormat.forPattern(DateHelper.YYYY_MM_DD_HH_MM_SS).parseDateTime(date);
        }
        return dateTime;
    }

    /**
     * 当前时间的毫秒数
     */
    public static long timestamp() {
        DateTime dateTime = new DateTime();
        long millis = dateTime.getMillis();
        return millis;
    }


    /**
     * 和当前系统时间相隔的分钟数
     */
    public static int minutesBetween(Date date) {
        // 系统当前时间
        DateTime SysTime = new DateTime();
        DateTime dateTime = null;
        if (date != null) {
            dateTime = new DateTime(date);
        }
        int minutes = Minutes.minutesBetween(dateTime, SysTime).getMinutes();
        return minutes;
    }

}
